<?php
	ini_set('display_errors', true);
	require_once(dirname(__FILE__) . "/../controllers/lega.php");
	require_once(dirname(__FILE__) . "/../controllers/squadra.php");
	require_once(dirname(__FILE__) . "/../controllers/calendari.php");
	require_once(dirname(__FILE__) . "/../model/db_driver.php");
	session_start();
	if(User::getCurrentUser()==null || User::getCurrentUser()->getRuolo()<User::$ADMIN){
		echo "Errore: devi essere un amministratore per visualizzare questa pagina.<br />";
		die();
	}
	if(isset($_POST['IsPostBack'])){
		//salva i dettagli modificati
		if(isset($_POST['btnSaveDetails'])){
			$l = null;
			if($_POST['btnSaveDetails']=='Salva'){
				$l = Lega::getLega($_POST['hdId']);
			}elseif($_POST['btnSaveDetails']=='Nuovo'){
				$l = new Lega(-1, '', '', 0,$_POST['cmbTipoLega']);
			}
			$l->setNome($_POST['txtNome']);
			$l->setStagione($_POST['txtStagione']);
			$l->setAttiva($_POST['cmbAbilitato']);
			$l->save();
		}
	}

	if(isset($_POST["lega"])){
		$cmd  = $_POST['cmd']; 
		if($cmd=='dettagli'){
			echo tabellaDettagli($_POST["lega"]);
		}elseif($cmd=='squadre'){
			echo tabellaSquadre($_POST['lega']);
		}elseif($cmd=='aggiungiSquadra'){
			Lega::addTeamToLeague($_POST['lega'], $_POST['squadra']);
			echo tabellaSquadre($_POST['lega']);
		}elseif($cmd=='eliminaSquadra'){
			Lega::removeTeamToLeague($_POST['lega'], $_POST['squadra']);
			echo tabellaSquadre($_POST['lega']);
		}elseif($cmd=='generaCalendario'){
			$l = Lega::getLega($_POST['lega']);
			$l->generaCalendario();
			echo tabellaDettagli($_POST["lega"]);
		}
		die();
	}

	function tabellaLeghe(){
		$leghe = Lega::caricaLeghe();
		
		$tab= "
		<table style='width:80%' cellpadding='0' cellspacing='0' border='0'>
			<thead>
				<tr>
					<td class='headerRow'>Id</td>
					<td class='headerRow'>Nome</td>
					<td class='headerRow'>Stagione</td>
					<td class='headerRow'>Attiva</td>
					<td class='headerRow'>Tipo lega</td>
					<td class='headerRow'>&nbsp;</td>
					<td class='headerRow'>&nbsp;</td>
				</tr>
			</thead>
		";
		for ($i=0;$i<count($leghe); $i++){
			$alt = $i % 2;
			$row_style = "rigaLega alt_" . $alt;
			$tab = $tab . "
				<tr>
					<td class='$row_style'>" . $leghe[$i]->getLegaId() ."</td>
					<td class='$row_style'>" . $leghe[$i]->getNome() ."</td>
					<td class='$row_style'>" . $leghe[$i]->getStagione() ."</td>
					<td class='$row_style'>" . ($leghe[$i]->isAttiva()==true ? "attiva" : "disattiva") . "</td>
					<td class='$row_style'>" . getTipoLega($leghe[$i]->getCompetizione()) . "</td>
					<td class='cc $row_style' style='text-decoration:underline;'
							onclick='showLeagueDetails(" . $leghe[$i]->getLegaId() .", \"dvLeagueDetail\")'>
						Dettagli
					</td>
					<td class='cc $row_style' style='text-decoration:underline;'
							onclick='showLeagueTeams(" . $leghe[$i]->getLegaId() .", \"dvLeagueTeams\")'>
						Squadre
					</td>
				</tr>
			";
		}
		
		return $tab . "</table>";
	}
	
	function getTipoLega($id){
		global $CALENDARIO_CAMPIONATO, $CALENDARIO_COPPA;
		if($id==$CALENDARIO_CAMPIONATO) {
			return "Campionato";
		}elseif($id==$CALENDARIO_COPPA){
			return "Coppa";
		}
	}
	
	function tabellaDettagli($id){
		$nome = "";
		$stagione="";
		$abilitato = true;
		$titolo = "Crea una nuova lega";
		$evento = "saveLeagueDetails();";
		$competizione = -1;
		if($id>=0){
			$l = Lega::getLega($id);
			$nome = $l->getNome();
			$stagione = $l->getStagione();
			$abilitato = $l->isAttiva();
			$competizione = $l->getCompetizione();
			$titolo = "Modifica i dettagli della lega";
		}else{
			$id = "Nuova";
			$evento = "newLeague();";
		}
		$details = "
		<table style='width:99%;font-size:10px;' cellpadding='0' cellspacing='0'>
			<tr>
				<td colspan='2' class='details' style='width:100%;text-align:center'>
					<p class='' style='color:green;font-weight:bold;font-size:14px;text-align:center;'> $titolo </p>
				</td>
			</tr>
			<tr>
				<td class='details ldetails'>Id</td>
				<td class='details'>$id<input type='hidden'  id='hdId'  name='hdId' value='$id' /></td>
			</tr>
			<tr>
				<td class='details ldetails'>Nome</td>
				<td class='details'><input type='text' class='txt' id='txtNome' name='txtNome' value='$nome' /></td>
			</tr>
			<tr>
				<td class='details ldetails'>Stagione</td>
				<td class='details'><input type='text' class='txt' id='txtStagione' name='txtStagione' value='$stagione' /> </td>
			</tr>
			<tr>
				<td class='details ldetails'>Abilitato</td>
				<td class='details'>
					" . getComboBoxAbilitato($abilitato) . "
				</td>
			</tr>
			<tr>
				<td class='details ldetails'>Tipo lega</td>
				<td class='details'>
					" . getComboTipoLega($competizione) . "
				</td>
			</tr>
			<tr>
				<td class='details ldetails'>Calendario</td>
				<td class='details'>
					<div style='color:blue;cursor:pointer;' name='btnMakeCal' onclick='makeCalendar($id)'><u>Genera calendario lega</u></div>
				</td>
			</tr>
			<tr>
				<td colspan='2' style='height:20px;'>&nbsp;</td>
			</tr>
			<tr>
				<td align='center' stsyle='text-align:center;'><input type='button' name='btnSaveDetails' value='Salva' onclick=' $evento closeDetails(" . "\"dvLeagueDetail\"" . ");' /></td>
				<td style='text-align:right;'><input type='button' name='btnCanc' value='Chiudi' onclick='closeDetails(" . "\"dvLeagueDetail\"" . ")' /></td>
			</tr>
		</table>
		";
		return $details;
	}
	
	function tabellaSquadre($id){
		$nome = "";
		$l = Lega::getLega($id);
		$nome = $l->getNome();
		$legaId = $l->getLegaId();
		$squadre = SquadraDesc::getSquadre($id);
		$no_squadre = SquadraDesc::getNoSquadre($id); //squadre che non appartengono alla lega
		$details = "
		<input type='hidden' name='hLeagueId' id='hLeagueId' value='$legaId' />
		<p class='' style='color:green;font-weight:bold;font-size:14px;text-align:center;'>
			Squadre della lega<br />$nome
		</p>
		
		<div style='width:100%;overflow-y:scroll;height:250px'>
		<table style='width:99%;font-size:10px;' cellpadding='0' cellspacing='0'>
			<tr>
				<td colspan='3'>
					&nbsp;
				</td>
			</tr>
		";
		if(count($squadre)==0){
			$details = $details . "
				<td colspan='2' class='details' style='width:100%;text-align:center'>
					Nessuna squadra in questa lega
				</td>			
			";
		}else{
			$details = $details . "
			<tr>
				<td class='headerRow'>Squadra</td>
				<td class='headerRow'>Allenatore</td>
				<td class='headerRow'>&nbsp;</td>
			</tr>
			";
			for($i=0;$i<count($squadre);$i++){
				$alt = $i % 2;
				$row_style = "rigaLega alt_" . $alt;
				$nome = $squadre[$i]->getNome();
				$squadraId = $squadre[$i]->getId();
				$allenatore = $squadre[$i]->getAllenatore();
				$details = $details . "
				<tr>
					<td class='$row_style'> $nome </td>
					<td class='$row_style'> $allenatore </td>
					<td class='cc $row_style' style='text-decoration:underline;' onclick='removeTeamToLeague($squadraId, \"$nome\");'>
						Elimina
					</td>
				</tr>";
			}
		}
				
		$details = $details . "
		</table>
		</div>
		<div style='width:100%;border-top:solid 1px gray;padding-top:20px;'>
			<table style='width:100%'>
		"
		;
		if(count($no_squadre)>0){
			$details = $details . "
					<tr>
						<td colspan='2'>
						<p class='' style='color:green;font-weight:bold;font-size:14px;text-align:center;'> Aggiungi squadra alla lega </p>
						</td>
					</tr>
					<tr>
						<td>
							" . getComboNoSquadre($no_squadre) . "
						</td>
						<td style='text-align:right;padding-right:10px;'>
							<input type='submit' style='width:85px;' name='btnAddTeam' id='btnAddTeam' value='Aggiungi' onclick='addTeamToLeague();' />
						</td>
					</tr>
				";
		}
		$details = $details . "
				<tr>
					<td colspan='2' style='width:100%;text-align:right;padding-right:10px;padding-top:20px;'>
						<input type='submit' style='width:85px;' name='btnCloseTeams' id='btnCloseTeams' value='Chiudi' onclick='closeDetails(\"dvLeagueTeams\")' />
					</td>
				</tr>
				</table>
			</div>
		";
		return $details;
	}
	
	function getComboBoxAbilitato($is_abilitato){
		$combo = "
			<select name='cmbAbilitato' id = 'cmbAbilitato' class='combo'>
				<option value='1' " . ($is_abilitato==true ? "selected='selected'" : "") . ">Abilitato</option>
				<option value='0' " . ($is_abilitato==false ? "selected='selected'" : "") . ">Disabilitato</option>
			";
		$combo = $combo . "</select>";
		return $combo;
	}
	
	function getComboTipoLega($competizioneId){
		global $CALENDARIO_CAMPIONATO, $CALENDARIO_COPPA;
		$combo = "
			<select name='cmbTipoLega' id = 'cmbTipoLega' class='combo'>
				<option value='$CALENDARIO_CAMPIONATO' " . ($competizioneId==$CALENDARIO_CAMPIONATO ? "selected='selected'" : "") . ">Campionato</option>
				<option value='$CALENDARIO_COPPA' " . ($competizioneId==$CALENDARIO_COPPA ? "selected='selected'" : "") . ">Coppa</option>
			</select>";
		return $combo;
	}	
	
	function getComboNoSquadre($no_squadre){
	/* Restitisce una combo box con le squadre non appartenenti alla lega corrente */
		$combo = "<select name='cmbNoSquadre' id='cmbNoSquadre' class='combo'> ";
		foreach($no_squadre as $s){
			$nome = $s->getNome();
			$id = $s->getId();
			$combo = $combo . "
				<option value='$id'>$nome</option>";
		}
		$combo = $combo . "
		</select>";
		return $combo;
	}


?>
	<script type='text/javascript' language='javascript'>
		
	</script>

	<style>
		td.headerRow {
			background-color:rgb(187,204,255);
			color:rgb(23,128,128);
			font-size:15px;
			border-right: solid 1px rgb(23,128,128);
			border-top: solid 1px rgb(23,128,128);
		
			padding-left: 3px;
			padding-right:3px;
			font-weight:bold;
		}
		
		td.rigaLega  {
			color:black;
			font-size:12px;
			border-right: solid 1px rgb(23,128,128);
			border-top: solid 1px rgb(23,128,128);
			padding-left: 3px;
			padding-right:3px;
			
		}

		td.alt_0{
			background-color:rgb(207,219,236);
		}
		
		td.alt_1{
			background-color:rgb(227,233,255);		
		}
		
		td.details{
			border-bottom:ridge 1px gray;
			border-right:ridge 1px gray;
			padding-bottom:3px;
			padding-left:3px;
			padding-top:3px;
		}
		
		td.ldetails{
			width:20%;
			text-align:right;
			padding-right:5px;
		}
		
		input.txt{
			width:95%;
			font-size:10px;
		}

			
		select.combo{
			font-size:10px;
			width:95%;
		}
	</style>
	<p  class='adminTitle'>
		Gestione leghe
	</p>
	<div>
		<button id='btnNewUser' onclick='showLeagueDetails(-1)'>Nuova Lega</button>
	</div>
	<br />
	<br />
	<!-- stampa la tabella degli utenti -->
	<?php echo tabellaLeghe(); ?>
	
	<!-- dettagli dell'utente -->
	<div id='dvLeagueDetail' style='background-color:#FAFAD2;display:none;width:400px; height:250px;border: solid 1px black;
		position:absolute;top:320px;left:450px'>

	</div>
	
	<div id='dvLeagueTeams' style='background-color:#FAFAD2;display:none;width:400px; height:450px;border: solid 1px black;
		position:absolute;top:120px;left:450px'>

	</div>
